package com.advertisementsystem.Mapper;

import com.advertisementsystem.entity.Notification;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface NotificationMapper extends BaseMapper<Notification> {

    @Select("<script>" +
            "SELECT * FROM notification " +
            "WHERE user_id = #{userId} " +
            "<if test='type != null and type != \"\"'> AND type = #{type} </if>" +
            "<if test='read != null'> AND `read` = #{read} </if>" +
            "ORDER BY created_time DESC" +
            "</script>")
    IPage<Notification> selectNotificationList(Page<Notification> page,
                                               @Param("userId") Long userId,
                                               @Param("type") String type,
                                               @Param("read") Integer read);

    @Select("SELECT COUNT(*) FROM notification WHERE user_id = #{userId} AND `read` = 0")
    int countUnreadNotifications(@Param("userId") Long userId);

    @Update("UPDATE notification SET `read` = 1 WHERE user_id = #{userId}")
    int markAllAsRead(@Param("userId") Long userId);
}